package com.ruoyi.job.task;

import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.job.kettle.KettleQuartz;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Component("runActiveJob")
public class RunActiveJob {
    private Logger logger = LoggerFactory.getLogger(RunActiveJob.class);

    @Autowired
    private RedisService redisService;

    public void ryNoParams() {
        logger.info("开始执行ACTIVE kettle job任务===================");

        Date date=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
        String currentday=sdf.format(new Date(date.getTime()));

        /**需要等待存储过程执行结束*/
        String actKey = "active-" + currentday + "-job";
        if ("0".equals(redisService.getCacheObject(actKey))) {
            logger.info("执行ACTIVE kettle job任务中===================");
        } else if("1".equals(redisService.getCacheObject(actKey))) {
            logger.info("执行ACTIVE kettle job已完成===================");
        } else {
            String proKey = "proc-" + currentday + "-QMCY_PERSONAL_INFO";
            if("1".equals(redisService.getCacheObject(proKey))) {
                logger.info("存储过程已执行完成，开始执行ACTIVE kettle job 任务==================");

                redisService.setCacheObject(actKey, "0");

                Map<String, String> map = new HashMap<String, String>();
                map.put("qmcy_path","/home/ETLitem");
                boolean bool = KettleQuartz.runKettleJob(map, "/home/ETLitem/ACTIVE_KETTLE.kjb");
                if(bool) {
                    redisService.setCacheObject(actKey, "1"); //执行完成
                    logger.info("执行ACTIVE kettle job完成===================");
                }
            } else {
                logger.info("请等待存储过程执行完成，再执行ACTIVE kettle job 任务==================");
            }
        }

        logger.info("结束执行ACTIVE kettle job任务===================");

    }
}
